AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:SpawnFunction(ply, tr)

	local SpawnPos = tr.StartPos + (tr.Normal*32)
	ent = ents.Create('ams_money')
	if !ent:IsValid() then return false end

	ent:SetPos(SpawnPos)
	ent:Spawn() --run ENT:Initialize()

	ent:Activate()

	return ent
end

function ENT:Initialize()
	
	local mass = 0
	if (self:GetModel() != "models/error.mdl") then
		//No Value Was Set
		if (self:GetModel() == "models/GTA IV/money_1.mdl") then
			//A big stack of ones
			self.value = 1000
			mass = 11.0
		elseif (self:GetModel() == "models/GTA IV/money_2.mdl") then
			//A big stack of 100'ths
			self.value = 50000
			mass = 12.0
		elseif (self:GetModel() == "models/GTA IV/money_3.mdl") then
			//A small stack of 100'ths
			self.value = 10000
			mass = 10.0
		elseif (self:GetModel() == "models/GTA IV/money_4.mdl") then
			//A 100
			mass = 7.0
			self.value = 100
		elseif (self:GetModel() == "models/GTA IV/money_pallet.mdl") then
			//A pallet...
			self.value = 100000
			mass = 190
		end
	else
		if (self.value == 1000) then
			self:SetModel("models/GTA IV/money_1.mdl")
			mass = 11.0
		elseif (self.value == 50000) then
			self:SetModel("models/GTA IV/money_2.mdl")
			mass = 12.0
		elseif (self.value == 10000) then
			self:SetModel("models/GTA IV/money_3.mdl")
			mass = 10.0
		elseif (self.value == 100) then
			self:SetModel("models/GTA IV/money_4.mdl")
			mass = 7.0
		elseif (self.value == 100000) then
			self:SetModel("models/GTA IV/money_pallet.mdl")
			mass = 190
		else
			self.value = 1000
			self:SetModel("models/GTA IV/money_1.mdl")
			mass = 11.0
		end
	end
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self:SetNetworkedInt( "value", 0 )

	//self:GetModel()
	self:SetNetworkedInt( "value", self.value )
    local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(mass)
	end
	
	self.damaged = 0
    self:SetMaxHealth(600)
	self:SetHealth(self:GetMaxHealth())

end

function ENT:GetTheValue()
	return self.value
end

function ENT:Use( activator, caller )
	if ( activator:IsPlayer() ) then
		--if (not activator:KeyDownLast(IN_USE)) then
			self:Remove()
			activator:Money_Add( self.value )
			activator:SendLua("GAMEMODE:AddNotify(\"Picked up "..self.value.." credits\", NOTIFY_GENERIC, 3)")
		--end
	end
end

function ENT:SetTheValue(newVal)
	self.value = newVal
	self:SetNetworkedInt( "value", self.value )
end

function ENT:OnRemove()
    self.BaseClass.OnRemove(self)
end

function ENT:TakeDamage(amount, attacker, inflictor)
	self:SetHealth(self:Health()-amount)
	if self:Health()<=0 then
		self:Destruct()
	end
end
/*
function ENT:Think()
    self.BaseClass.Think(self)
	self.Entity:NextThink( CurTime() + 1 )
	return true
end
*/